package com.ef.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.MarketingPlanDao;
import com.ef.domain.MarketingPlan;
import com.ef.service.MarketingPlanService;

public class MarketingPlanServiceImpl extends BaseServiceImpl implements
		MarketingPlanService {

	private MarketingPlanDao marketingPlanDao;

	public MarketingPlanDao getMarketingPlanDao() {
		return marketingPlanDao;
	}

	public void setMarketingPlanDao(MarketingPlanDao marketingPlanDao) {
		this.marketingPlanDao = marketingPlanDao;
	}

	public void saveOrUpdate(MarketingPlan entity) {
		//marketingPlanDao.clear();
		marketingPlanDao.saveOrUpdate(entity);
	}

	public MarketingPlan findById(Long id) {
		return marketingPlanDao.findById(id);
	}

	public List<MarketingPlan> getPlanBetweenDate(Date fdate, Date ldate) {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(MarketingPlan.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.add(Restrictions.ge("startDate", fdate));
		criterion.add(Restrictions.le("startDate", ldate));
		criterion.addOrder(Order.asc("startDate"));
		return marketingPlanDao.findAllByCriteria(criterion);
	}

	public List<MarketingPlan> getAdChannelOtherPlanBetweenDate(Date fdate,
			Date ldate, Long adChannelId, Long planId) {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(MarketingPlan.class);
		criterion.createCriteria("adChannel").add(
				Restrictions.eq("id", adChannelId));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		Disjunction or = Restrictions.disjunction();
		Conjunction and1 = Restrictions.conjunction();
		and1.add(Restrictions.ge("startDate", fdate));
		and1.add(Restrictions.le("startDate", ldate));
		Conjunction and2 = Restrictions.conjunction();
		and2.add(Restrictions.ge("endDate", fdate));
		and2.add(Restrictions.le("endDate", ldate));
		Conjunction and3 = Restrictions.conjunction();
		and3.add(Restrictions.le("startDate", fdate));
		and3.add(Restrictions.ge("endDate", ldate));
		or.add(and1);
		or.add(and2);
		or.add(and3);
		if (planId != null) {
			criterion.add(Restrictions.ne("id", planId));
		}
		criterion.add(or);
		criterion.addOrder(Order.asc("startDate"));
		return marketingPlanDao.findAllByCriteria(criterion);
	}

	public List<MarketingPlan> getAdChannelPlanBetweenDate(Date fdate,
			Date ldate, Long adChannelId) {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(MarketingPlan.class);
		criterion.createCriteria("adChannel").add(
				Restrictions.eq("id", adChannelId));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.add(Restrictions.ge("startDate", fdate));
		criterion.add(Restrictions.le("startDate", ldate));
		criterion.addOrder(Order.asc("startDate"));
		return marketingPlanDao.findAllByCriteria(criterion);
	}

	public List<MarketingPlan> findByAnything(String qitem, int max) {
		StringBuffer hql = new StringBuffer(
				"Select marketingPlan From MarketingPlan as marketingPlan "
						+ " inner join marketingPlan.adChannel as adChannel  "
						+ "where (lower(adChannel.name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(adChannel.pyname) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%'  or lower(marketingPlan.name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and marketingPlan.status='VALID' ");
		return marketingPlanDao.findMultiAll(hql.toString(), 0, max);
//		DetachedCriteria criterion = DetachedCriteria
//				.forClass(MarketingPlan.class);
//		Disjunction or = Restrictions.disjunction();
//		criterion.createCriteria("adChannel", "AD");
//		Property myPropertyAd = Property.forName("AD.name");
//		or.add(myPropertyAd.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myPropertyAd.like("%" + qitem.toUpperCase() + "%"));
//		myPropertyAd = Property.forName("AD.pyname");
//		or.add(myPropertyAd.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myPropertyAd.like("%" + qitem.toUpperCase() + "%"));
//		Property myProperty = Property.forName("name");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		criterion.add(or);
//		criterion.add(Restrictions.eq("status", StatusType.VALID));
//		return marketingPlanDao.findAllByCriteria(criterion, max);
	}

	public List<MarketingPlan> findByAnythingByTeamId(String qitem, int max,Long teamid){
		StringBuffer hql = new StringBuffer(
				"Select marketingPlan From MarketingPlan as marketingPlan "
						+ " inner join marketingPlan.adChannel as adChannel  "
						+ "where (lower(adChannel.name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(adChannel.pyname) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%'  or lower(marketingPlan.name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and marketingPlan.status='VALID' and adChannel.team.id=");
		hql.append(teamid);
		return marketingPlanDao.findMultiAll(hql.toString(), 0, max);
	}
	
	public void delete(MarketingPlan entity) {
		entity.setStatus(StatusType.INVALID);
		marketingPlanDao.update(entity);
	}
}
